Ext.Loader.setPath("FormFieldUtil",contextPath+"ui_tpl/FormFieldUtil.js");
Ext.define("Ext.ux.SingleForm",{
	extend:'Ext.form.Panel',
	xtype:'singleform',
	bodyPadding: 15,
	scrollable:true,
	requires:[
		'FormFieldUtil'
	],
	layout: 'anchor',
	defaults: {
	        anchor: '100%'
	 },
	defaultType: 'textfield',
	initComponent:function(){
		var me=this,
		metaData=this.metaData,
		refMetaMap=metaData["refMetaMap"],
		mainMetaId=this.mainMetaId,
		mainMeta=this.mainMeta=refMetaMap[mainMetaId],
		attrs=this.attrs=mainMeta["attrs"],
		fields=this.fields=mainMeta["fields"];
		var action=attrs["action"]||{url:contextPath+"meta/record/"+mainMetaId,type:"rest"};
		this.url=action["url"];
		this.idProperty=this.attrs["idProperty"];
		this.loadRecordUrl=attrs["loadRecordUrl"]||(contextPath+"meta/singleRecord/"+mainMetaId);
		me.initModel();
		me.initButtons();
		me.items=FormFieldUtil.getFormFields(mainMetaId,metaData);
		me.items.push({
			xtype:"numberfield",
			name:'id',
			hidden:true
		});
		this.callParent();
	},
	initModel:function(){
		var modelName=this.modelName=this.modelName||("Ext.ux."+this.mainMeta["code"]);
		if(!Ext.ClassManager.isCreated(modelName)){
			var typeMap={"1":"string","-5":"int","4":"int","12":"string","16":"boolean","91":"date","93":"date","97":"date"},
			fields=this.fields,modelFields=[];
			Ext.iterate(fields,function(field,i){
				
				var type=field["type"],name=field["name"],
				attrs=field["attrs"],
				f={name:name,type:typeMap[field["type"]]},
				valid={};
				if(attrs["required"]){
					valid[name]="presence";
				}
				if(attrs["defaultValue"]){
					f["defaultValue"]=attrs["defaultValue"];
				}else{
					f["defaultValue"]=null;
				}
				modelFields.push(f);
			});
			Ext.define(modelName,{
				extend:"Ext.data.Model",
				idProperty:attrs["idProperty"]||"id",
				fields:modelFields
			});
		}
	},
	listeners:{
		'afterrender':function(me){
			me.loadData();
		}
	},
	loadData:function(){
		var me=this,params={};
		if(me.globalId){
			params={id:me.globalId};
		}
		Ext.Ajax.request({
			url:me.loadRecordUrl,
			method:"GET",
			params:params,
			success: function(response, opts) {
		         var obj = Ext.decode(response.responseText);
		         if(obj["success"]){
		        	 var data=obj["data"];
		        	 data["id"]=me.globalId;
		        	 var rec=Ext.create(me.modelName,data);
		        	 me.loadRecord(rec);
		         }
		     },

		     failure: function(response, opts) {
		    	 Ext.toast('提示','数据加载失败');
		     }
		});
	},
	initButtons:function(){
		var me=this;
		this.buttons=[{
	        text: '刷新',
	        iconCls:"fa fa-refresh fa-fw",
	        handler: function() {
	            this.up('form').loadData();
	        }
	    }, {
	        text: '提交',
	        iconCls:"fa fa-arrow-up",
	        formBind: true,
	        disabled: true,
	        handler: function() {
	            var form = this.up('form').getForm();
	            form.findField('id').setValue(me.globalId);
	            if (form.isValid()) {
	                form.submit({
	                    success: function(form, action) {
	                    	Ext.toast("提交成功",'提示', 't');
	                    	
	                    },
	                    failure: function(form, action) {
	                    	Ext.toast(action.result.message,'提示', 't');
	                    }
	                });
	            }
	        }
	    }];
	}
});

